package com.hello.sky.controller.user;

import com.hello.sky.model.dto.ShoppingCartDTO;
import com.hello.sky.model.entity.ShoppingCart;
import com.hello.sky.result.Result;
import com.hello.sky.service.ShoppingCartService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingController {

    @Autowired
    private ShoppingCartService cartService;

    @PostMapping("/add")
    public Result addIntoCart(@RequestBody ShoppingCartDTO dto){
        cartService.addIntoCart(dto);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getCart(HttpServletRequest request){
       List<ShoppingCart> carts = cartService.getCartByUserId(request);
       return Result.success(carts);
    }

    @PostMapping("/sub")
    public Result subItem(@RequestBody ShoppingCartDTO dto){
        cartService.subItem(dto);
        return Result.success();
    }

    @DeleteMapping("/clean")
    public Result cleanCart(){
        cartService.cleanCart();
        return Result.success();
    }
}
